package xpn.platform.modules.todo.wsdl;

import lombok.extern.slf4j.Slf4j;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Service;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import xpn.platform.common.config.XpnPlatformConfig;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * 多米诺平台  代办/已办 远程调用工具
 *
 * @author duwei
 * @version 2018/3/22 10:29
 */
@Slf4j
public class TodosCall {

    @Autowired
    XpnPlatformConfig xpnPlatformConfig;

    private static final String DMN_WS_URL = "http://10.223.2.71/Todos.nsf/Todos?OpenWebService";
    private static final String WS_USER_NAME = "xpn";
    private static final String WS_PWD = "xpn2018ok";

    private DominoSoapBindingStub stub;
    private String token;

    public TodosCall() {
        try {
            Service service = new Service();
            stub = new DominoSoapBindingStub(new URL(DMN_WS_URL), service);
            stub.setTimeout(1000 * 60 * 20);
        } catch (MalformedURLException | AxisFault e) {
            e.printStackTrace();
            log.error("连接多米诺服务失败!", e);
        }
    }

    private String getToken() throws RemoteException {
        if (StringUtils.isEmpty(token)) {
            token = this.stub.login(WS_USER_NAME, WS_PWD);
        }
        return token;

    }

    /**
     * 创建待办
     *
     * @param linkUrl    链接Url
     * @param senderId   发送人Id(实际为发送人code) eg.01/005/9
     * @param senderName 发送人名称 eg.陈明辉/办公室/黔南州中心支行
     * @param subject    标题
     * @param category   待办分类
     * @param emergency  紧急程度
     * @param sendeeList 接收人列表 用分号分割 eg.15/10/10/1;15/10/10/2;
     */
    public void create(String linkUrl, String senderId, String senderName, String subject, String category, String emergency, String sendeeList) throws RemoteException, UnsupportedEncodingException {
        this.stub.create(getToken(), constructLink(linkUrl), senderId, senderName, subject, category, emergency, sendeeList);
    }

    /**
     * 删除待办
     *
     * @param linkUrl    链接Url
     * @param sendeeList 接收人列表 用分号分割
     * @throws RemoteException
     */
    public void delete(String linkUrl, String sendeeList) throws RemoteException, UnsupportedEncodingException {
        this.stub.delete(getToken(), constructLink(linkUrl), sendeeList);
    }

    /**
     * 完成待办(待办移入已办)
     *
     * @param linkUrl    链接Url
     * @param sendeeList 接收人列表 用分号分割
     */
    public void complete(String linkUrl, String sendeeList) throws RemoteException, UnsupportedEncodingException {
        this.stub.complete(getToken(), constructLink(linkUrl), sendeeList);
    }

    /**
     * 已办还原（已办变待办）
     *
     * @param linkUrl    链接Url
     * @param sendeeList 接收人列表 用分号分割
     */
    public void revert(String linkUrl, String sendeeList) throws RemoteException, UnsupportedEncodingException {
        this.stub.revert(getToken(), constructLink(linkUrl), sendeeList);
    }

    /**
     * 构造link
     *
     * @return
     */
    private String constructLink(String link) throws UnsupportedEncodingException {

        Pattern pattern = compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
        if (pattern.matcher(link).matches()) {
            return link;
        }

        String prefix = xpnPlatformConfig.getTodoLink().getOpenForm();
        String encodeUrl = URLEncoder.encode(link,"UTF-8");
        return prefix + "?linkUrl=" + encodeUrl;
    }
}
